#include <iostream>

using namespace std;

/*
 * 浅拷贝  简单的赋值拷贝操作(编译器提供的拷贝函数都是浅拷贝)
 * 深拷贝  在堆区重新申请空间，进行拷贝操作
 */

class Person{
public:
    int p_age;
    int *p_height;

    Person(int age, int height) {
        p_age = age;
        p_height = new int(height);
    }

    Person(const Person &p){
        cout << "析构函数" <<endl;
        p_age = p.p_age;
//        p_height = p.p_height;  // 默认拷贝构造提供此类辅助语句，应该成深拷贝
        p_height = new int(*p.p_height);
    }

    ~Person(){
        if(p_height != NULL){   // 在析构函数中释放堆区开辟的内存
            delete(p_height);
            p_height = NULL;
        }
        cout << "Person析构函数调用" << endl;
    }
};

void test01(){
    Person p1(10,160);
    cout << "age= " << p1.p_age << ", height= " << *p1.p_height << endl;

    Person p2(p1);
    cout << "age= " << p2.p_age << ", height= " << *p2.p_height << endl;
}

int main() {
    test01();

    return 0;
}
